<?php

class user_couponModel extends Model {

	public function getList($where, $value, $page, $pageSize) {
		$limit = ($page - 1) * $pageSize;
		$resultData = [
			'page' => $page,
			'pageSize' => $pageSize,
			'list' => [],
		];
		if (!$list = $this->order('coupon_id desc')->limit($limit, $pageSize)->getAll($where, $value)) {
			return $resultData;
		}
		$shopInfo = [];
		$listtemp = '';
		$shopIds = [];
		foreach ($list as $key => $value) {
			$listtemp[$key]['id'] = intval($value['id']);
			$listtemp[$key]['uid'] = intval($value['uid']);
			$listtemp[$key]['coupon_id'] = intval($value['coupon_id']);
			$listtemp[$key]['coupon_name'] = $value['coupon_name'];
			$listtemp[$key]['type'] = intval($value['type']);
			$listtemp[$key]['rebate'] = intval($value['rebate']);
			$listtemp[$key]['min_price'] = intval($value['min_price']);
			$listtemp[$key]['price'] = intval($value['price']);
			$listtemp[$key]['status'] = intval($value['status']);
			$listtemp[$key]['s_time'] = date('Y.m.d', $value['s_time']);
			$listtemp[$key]['e_time'] = date('Y.m.d', $value['e_time']);
			$listtemp[$key]['isnew'] = $value['isnew'] == 1 ? 1 : 0;
			$listtemp[$key]['shopid'] = intval($value['shopid']);
			$listtemp[$key]['shopInfo'] = $shopInfo;
			if (intval($value['shopid'])) {
				$shopIds[] = intval($value['shopid']);
			}
		}
		$shopIds = implode(',', $shopIds);
		if ($shopIds) {
			$shopList = self::model('shop')->query("select * from wf_shop where shop_id in ({$shopIds})")->fetchAll();
			foreach ($listtemp as $key => $value) {
				foreach ($shopList as $key1 => $value1) {
					if ($value['shopid'] == $value1['shop_id']) {
						$shopInfos = [
							'shopId' => intval($value1['shop_id']),
							'name' => strval($value1['name']),
							'image' => strval($value1['image']),
							'avatar' => strval($value1['avatar']),
						];
						$listtemp[$key]['shopInfo'] = [
							'shopId' => intval($value1['shop_id']),
							'name' => strval($value1['name']),
							'image' => strval($value1['image']),
							'avatar' => strval($value1['avatar']),
						];
					}
				}
			}
		}

		$resultData['list'] = $listtemp;
		return $resultData;
	}
}